<html>
<head><meta charset="utf-8"><title>stacked borrows: Transmute &amp;UnsafeCell&lt;T&gt; to T · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/stacked.20borrows.3A.20Transmute.20.26UnsafeCell.3CT.3E.20to.20T.html">stacked borrows: Transmute &amp;UnsafeCell&lt;T&gt; to T</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="147873498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/stacked%20borrows%3A%20Transmute%20%26UnsafeCell%3CT%3E%20to%20T/near/147873498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/stacked.20borrows.3A.20Transmute.20.26UnsafeCell.3CT.3E.20to.20T.html#147873498">(Nov 17 2018 at 10:53)</a>:</h4>
<p>Stacked Borrows currently declares a transmute from <code>&amp;UnsafeCell&lt;T&gt;</code> to <code>&amp;T</code> illegal. You have to transmute to a raw pointer, and then <code>&amp;*</code> that. The reason is that an <code>&amp;T</code> expects the location to be frozen <em>since it was created</em>, and since you transmuted it's still the same pointer, so the time of creation is when you created that <code>&amp;UnsafeCell&lt;T&gt;</code> -- but at that point, the location was not frozen. If, instead, you go through a raw pointer (<code>&amp;*(UnsafeCell&lt;T&gt; as *const UnsafeCell&lt;T&gt; as *const T)</code>), you create a <em>new</em> shared reference and hence the model only expects things to be frozen since then, which they are.<br>
This was actually violated in <code>UnsafeCell::get</code>, but only because <code>&amp;x.field as *const _</code>, where <code>x: &amp;UnsafeCell&lt;T&gt;</code>, creates an intermdiate <code>&amp;T</code> (which it will no more with <a href="https://github.com/rust-lang/rfcs/pull/2582" target="_blank" title="https://github.com/rust-lang/rfcs/pull/2582">https://github.com/rust-lang/rfcs/pull/2582</a>). However, it is also violated <a href="https://github.com/carllerche/bytes/blob/456221d16521cf54cea0e6569669e47120a1b738/src/bytes.rs#L2441" target="_blank" title="https://github.com/carllerche/bytes/blob/456221d16521cf54cea0e6569669e47120a1b738/src/bytes.rs#L2441">in the bytes crate</a>, which just blatantly transmutes an <code>&amp;AtomicPtr</code> to an <code>&amp;u8</code>.</p>



<a name="147875054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/stacked%20borrows%3A%20Transmute%20%26UnsafeCell%3CT%3E%20to%20T/near/147875054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/stacked.20borrows.3A.20Transmute.20.26UnsafeCell.3CT.3E.20to.20T.html#147875054">(Nov 17 2018 at 11:46)</a>:</h4>
<p>Also see <a href="https://github.com/carllerche/bytes/pull/233" target="_blank" title="https://github.com/carllerche/bytes/pull/233">https://github.com/carllerche/bytes/pull/233</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>